package study.javaee.practice.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import study.javaee.practice.common.BO.Result;
import study.javaee.practice.dao.ProjectDao;
import study.javaee.practice.entity.Project;
import javax.annotation.Resource;

@RestController
@RequestMapping("/project")
public class ProjectManageController {
    @Resource
    ProjectDao projectdao;

    @PutMapping("/plus/{id}")
    public Result<?> plus(@PathVariable Long id) {
        UpdateWrapper<Project> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("id",id).set("state", 1);
        projectdao.update(null,updateWrapper1);
        return Result.success();
    }

    @PutMapping("/subtract/{id}")
    public Result<?> subtract(@PathVariable Long id) {
        UpdateWrapper<Project> updateWrapper2 = new UpdateWrapper<>();
        updateWrapper2.eq("id",id).set("state", 0);
        projectdao.update(null,updateWrapper2);
        return Result.success();
    }

    @PutMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id) {
        UpdateWrapper<Project> updateWrapper3 = new UpdateWrapper<>();
        updateWrapper3.eq("id",id).set("state", 3);
        projectdao.update(null,updateWrapper3);
        return Result.success();
    }


    @GetMapping
    /*新增接口findPage*/
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Project> wrapper = Wrappers.<Project>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Project::getName, search)
                    .between(Project::getState, 0,2);
        } else {
            wrapper.between(Project::getState, 0,2);
        }
        Page<Project> projectPage = projectdao.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(projectPage);
    }
}
